/*
 * Copyright 2008-2009 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public Licence Version 3
 * The licence can be read in its entirety in the LICENSE.txt file accompaning this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of WeightWatch.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package net.cachapa.weightwatch.ui;

import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.TabbedPane;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.SelectionListener;
import com.sun.lwuit.layouts.GridLayout;

public class PreferencesForm extends Form implements ActionListener, SelectionListener {
	public Application application;
	private TabbedPane tabbedPane;
	private UserPreferencesContainer userContainer;
	private DatabasePreferencesContainer databaseContainer;
	private LocalizationPreferencesContainer localizationContainer;
	private AboutContainer aboutContainer;
	private Command backCommand;

	public PreferencesForm(Application application) {
		this.application = application;
		this.setTitle("pref_title");
		this.setScrollable(false);	// Scrolling will be handled by each tab's container
		this.setLayout(new GridLayout(1, 1));
		this.setFocusable(false);	// So that the tabbedPane never loses focus
		
		this.tabbedPane = new TabbedPane();
		
		userContainer = new UserPreferencesContainer(application);
		tabbedPane.addTab(null, userContainer.getIcon(), userContainer);
		
		databaseContainer = new DatabasePreferencesContainer(application);
		tabbedPane.addTab(null, databaseContainer.getIcon(), databaseContainer);
		
		localizationContainer = new LocalizationPreferencesContainer(application);
		tabbedPane.addTab(null, localizationContainer.getIcon(), localizationContainer);
		
		aboutContainer = new AboutContainer(application);
		tabbedPane.addTab(null, aboutContainer.getIcon(), aboutContainer);
		
		this.addComponent(tabbedPane);
		tabbedPane.addTabsListener(this);
		
		backCommand = new Command("back");
		this.setBackCommand(backCommand);
		this.addCommand(backCommand);
		this.setCommandListener(this);
	}
	
	public void show() {
		this.databaseContainer.show();
		this.userContainer.show();
		this.localizationContainer.show();
		super.show();
	}

	public void actionPerformed(ActionEvent ae) {
		userContainer.saveUserHeight();
		if (ae.getSource() == backCommand) {
			application.performAction(Application.SHOW_MAIN_FORM);
		}
	}

	public void selectionChanged(int oldSelected, int newSelected) {
//		PreferencesContainer oldTab = (PreferencesContainer)tabbedPane.getTabComponentAt(oldSelected);
//		PreferencesContainer newTab = (PreferencesContainer)tabbedPane.getTabComponentAt(newSelected);
//		tabbedPane.setTabTitle(null, oldTab.getIcon(), oldSelected);
//		tabbedPane.setTabTitle(newTab.getName(), newTab.getIcon(), newSelected);
	}
}
